#include "AppUtils.h"
#include "AppController.h"

void AppUtils::setAdInvisible(bool isInvisible) {
    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
    
    if (isInvisible) {
        [appController setAdVisible:YES];
    } else {
        [appController setAdVisible:NO];
    }
}

void AppUtils::showDialog(char* titleStr, char* contentStr, char* doneTextStr) {
    NSString* title = [NSString stringWithUTF8String:titleStr];
    NSString* content = [NSString stringWithUTF8String:contentStr];
    NSString* doneText = [NSString stringWithUTF8String:doneTextStr];
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:title
                          message:content
                          delegate:nil
                          cancelButtonTitle:doneText
                          otherButtonTitles:nil,
                          nil];
    [alert show];
}

void AppUtils::showAdFullscreen() {
    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
    [appController showFullscreenAd];
}

void AppUtils::showLeaderboard() {
    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
    [appController showLeaderboard];
}

void AppUtils::reportScore(int score, char* category) {
    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
    NSString* nsCategory = [NSString stringWithUTF8String:category];
    [appController reportScore: score forLeaderboardID:nsCategory];
}

void AppUtils::openForRate() {
    NSString* link = @"itms-apps://itunes.apple.com/app/id926654548";
    if ( [[UIDevice currentDevice].systemVersion floatValue] < 7.0) {
        link = @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=926654548";
    }
    NSURL* nsUrl = [NSURL URLWithString:link];
    [[UIApplication sharedApplication] openURL:nsUrl];
}

void AppUtils::shareFacebook(std::string title, std::string caption, std::string imgUrl, std::string linkUrl, std::string msg) {
    NSString* nsTitle = [NSString stringWithUTF8String:title.data()];
    NSString* nsCaption = [NSString stringWithUTF8String:caption.data()];
    NSString* nsImgUrl = [NSString stringWithUTF8String:imgUrl.data()];
    NSString* nsLinkUrl = [NSString stringWithUTF8String:linkUrl.data()];
    NSString* nsMsg = [NSString stringWithUTF8String:msg.data()];
    
    AppController* appController = (AppController*) [UIApplication sharedApplication].delegate;
    [appController shareFb:nsTitle withCaption:nsCaption withImage:nsImgUrl withUrl:nsLinkUrl errorMsg:nsMsg];
}
void AppUtils::shareTwitter(std::string title, std::string caption, std::string description, std::string imgUrl, std::string linkUrl, std::string msg) {
    NSString* nsTitle = [NSString stringWithUTF8String:title.data()];
    NSString* nsCaption = [NSString stringWithUTF8String:caption.data()];
    NSString* nsDescription = [NSString stringWithUTF8String:description.data()];
    NSString* nsImgUrl = [NSString stringWithUTF8String:imgUrl.data()];
    NSString* nsLinkUrl = [NSString stringWithUTF8String:linkUrl.data()];
    NSString* nsMsg = [NSString stringWithUTF8String:msg.data()];
    
    AppController* appController = (AppController*) [UIApplication sharedApplication].delegate;
    [appController shareTw:nsTitle withCaption:nsCaption withDescription:nsDescription withImage:nsImgUrl withUrl:nsLinkUrl errorMsg:nsMsg];
}

bool AppUtils::isRemovedAds() {
    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
    return [appController isRemovedAds];
}

//void AppUtils::buyInApp() {
//    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
//    [appController buyInApp];
//}
//
//void AppUtils::restoreInApp() {
//    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
//    [appController restoreInApp];
//}
/*

void AppUtils::openForUpgrade() {
    NSURL* nsUrl = [NSURL URLWithString:@"https://itunes.apple.com/us/app/flappy-birds-escape-pro/id887894525?ls=1&mt=8"];
    [[UIApplication sharedApplication] openURL:nsUrl];
}

void AppUtils::buyInApp() {
    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
    [appController buyInApp];
}

void AppUtils::restoreInApp() {
    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
    [appController restoreInApp];
}

void AppUtils::setPlaying(bool playing) {
    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
    [appController setPlaying:playing];
}

bool AppUtils::checkShareSuccess() {
    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
    BOOL shareSuccess = [appController isShareSuccess];
    if (shareSuccess == true) {
        return true;
    } else {
        return false;
    }
}

void AppUtils::setShareSuccess(bool isSuccess) {
    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
    [appController setShareSuccess:isSuccess];
}

bool AppUtils::checkPurchaseSuccess() {
    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
    BOOL purchaseSuccess = [appController isPurchaseSuccess];
    if (purchaseSuccess == true) {
        return true;
    } else {
        return false;
    }
}

void AppUtils::setPurchaseSuccess(bool isSuccess) {
    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
    [appController setPurchase:isSuccess];
}

void AppUtils::shareFacebook(char* title, char* imgUrl, char* linkUrl, char* msg) {
    NSString* nsTitle = [NSString stringWithUTF8String:title];
    NSString* nsImgUrl = [NSString stringWithUTF8String:imgUrl];
    NSString* nsLinkUrl = [NSString stringWithUTF8String:linkUrl];
    NSString* nsMsg = [NSString stringWithUTF8String:msg];
    
    AppController* appController = (AppController *)[UIApplication sharedApplication].delegate;
    [appController shareFb:nsTitle withImage:nsImgUrl withUrl:nsLinkUrl errorMsg:nsMsg];
}

void AppUtils::shareTwitter(char* title, char* imgUrl, char* linkUrl, char* msg) {
    NSString* nsTitle = [NSString stringWithUTF8String:title];
    NSString* nsImgUrl = [NSString stringWithUTF8String:imgUrl];
    NSString* nsLinkUrl = [NSString stringWithUTF8String:linkUrl];
    NSString* nsMsg = [NSString stringWithUTF8String:msg];
    
    AppController* appController = (AppController *)[UIApplication sharedApplication].delegate;
    [appController shareTw:nsTitle withImage:nsImgUrl withUrl:nsLinkUrl errorMsg:nsMsg];
}*/

bool AppUtils::isLoaddedBanner() {
    AppController *appController = (AppController *)[UIApplication sharedApplication].delegate;
    BOOL isLoaddedBanner = [appController isLoadedBanner];
    if (isLoaddedBanner == true) {
        return true;
    } else {
        return false;
    }
}
